package com.loubii.account.ui.avtivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loubii.account.R;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;

public class ComputedIncomeActivity extends BaseToolBarActivity {

    private final String TAG = "content";
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_computed_income;
    }

    @Override
    protected void initView() {
        setTitle("收益计算");
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_card_detail_change, menu);
//        return true;
//    }
    @OnClick(R.id.computer_btn)
    public void beginComputed(View view) {
        Log.d(TAG, "beginComputed: ");
        EditText et = (EditText)findViewById(R.id.Price);
        EditText total_income_val = (EditText)findViewById(R.id.total_income_val);
        EditText hold_day = (EditText)findViewById(R.id.hold_day);
        if(et.getText().toString().equals("")||total_income_val.getText().toString().equals("")||total_income_val.getText().toString().equals("")){

            Toast toast = new Toast(this);
            Toast.makeText(this, "请补全信息!!!", Toast.LENGTH_LONG).show();
            return;
        }
        BigDecimal inver_val = new BigDecimal(et.getText().toString());
        BigDecimal income_val = new BigDecimal(total_income_val.getText().toString());
        BigDecimal hold_day_val = new BigDecimal(hold_day.getText().toString());

        ArrayList res = this.income_ratio(inver_val, income_val,hold_day_val);
        TextView tv1 = (TextView)findViewById(R.id.tatol_ratio);
        TextView tv2 = (TextView)findViewById(R.id.tatol_year_ratio);
        String etv1 = res.get(0).toString();
        String etv2 = res.get(1).toString();
        tv1.setText(etv1);
        tv2.setText(etv2);
        Log.d(TAG, "etv: " + hold_day_val);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        startActivity(new Intent(mContext, AccountRemindAddActivity.class));
        return super.onOptionsItemSelected(item);
    }

    public ArrayList income_ratio(BigDecimal invert, BigDecimal income,BigDecimal hold_day){

        BigDecimal per = new BigDecimal(100);
        BigDecimal year = new BigDecimal(36500);
        ArrayList<BigDecimal> ratio_list =  new ArrayList<>();
        BigDecimal total_ratio = income.divide(invert,4, RoundingMode.HALF_UP).multiply(per);
        if(invert.compareTo(income) == 1){
            total_ratio = total_ratio.negate();
        }
        BigDecimal shouyi = income.subtract(invert);
        BigDecimal fenzi = shouyi.divide(invert,4, RoundingMode.HALF_UP);
        BigDecimal fenmu = hold_day.divide(new BigDecimal(365),4, RoundingMode.HALF_UP);
        BigDecimal total_year_ratio = fenzi.divide(fenmu,4, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
        ratio_list.add(total_ratio.setScale(2,RoundingMode.HALF_UP));
        ratio_list.add(total_year_ratio.setScale(2,RoundingMode.HALF_UP));
        // 网站 http://data.eastmoney.com/money/calc/CalcFundSY.html
        // 计算公式（收益/本金）/(持有天数/365)  转换成百分比再乘100

        return ratio_list;
    }
}
